﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data.Linq;

using EnglishRegister.Entities;
using EnglishRegister.Utilities;

namespace EnglishRegister.Business
{
    public class CourseBL
    {
        public static IList GetAllCourse()
        {
            var query = from q in CourseModel.GetTable()
                        select new
                        {
                            CourseID = q.ID,
                            CourseName = q.Name.Trim(),
                            Level = Enum.GetName(typeof(CourseLevel), q.Level),
                            LitmitPeople = q.LimitPeople,
                            StartTime = q.StartTime,
                            EndTime = q.EndTime,
                            TimeStudy = Enum.GetName(typeof(CourseLearnTime), q.TimeStudy == null ? 0 : q.TimeStudy)
                        };
            return query.ToList();
        }

        public static bool IsFullClass(Guid courseID)
        {
            int studentCount = StudentBL.GetAllStudentByCourse(courseID).Count;
            return studentCount >= GetByPrimaryKey(courseID).LimitPeople;
        }

        public static IList GetAllCourseByFilter(FiIterType filter, string strName)
        {
            switch (filter)
            {
                case FiIterType.All:
                    return SearchCourseByName(strName);
                case FiIterType.StartingClass:
                    var query1 = from q in CourseModel.GetTable()
                                 where q.StartTime > DateTime.Now && q.Name.StartsWith(strName)
                                 select new
                                 {
                                     CourseID = q.ID,
                                     CourseName = q.Name.Trim(),
                                     Level = Enum.GetName(typeof(CourseLevel), q.Level),
                                     LitmitPeople = q.LimitPeople,
                                     StartTime = q.StartTime,
                                     EndTime = q.EndTime,
                                     TimeStudy = Enum.GetName(typeof(CourseLearnTime), q.TimeStudy == null ? 0 : q.TimeStudy)
                                 };
                    return query1.ToList();
                case FiIterType.TimeEven:
                    var query2 = from q in CourseModel.GetTable()
                                 where q.TimeStudy == 0 && q.Name.StartsWith(strName)
                                 select new
                                 {
                                     CourseID = q.ID,
                                     CourseName = q.Name.Trim(),
                                     Level = Enum.GetName(typeof(CourseLevel), q.Level),
                                     LitmitPeople = q.LimitPeople,
                                     StartTime = q.StartTime,
                                     EndTime = q.EndTime,
                                     TimeStudy = Enum.GetName(typeof(CourseLearnTime), q.TimeStudy == null ? 0 : q.TimeStudy)
                                 };
                    return query2.ToList();
                case FiIterType.TimeUneven:
                    var query3 = from q in CourseModel.GetTable()
                                 where q.TimeStudy == 1 && q.Name.StartsWith(strName)
                                 select new
                                 {
                                     CourseID = q.ID,
                                     CourseName = q.Name.Trim(),
                                     Level = Enum.GetName(typeof(CourseLevel), q.Level),
                                     LitmitPeople = q.LimitPeople,
                                     StartTime = q.StartTime,
                                     EndTime = q.EndTime,
                                     TimeStudy = Enum.GetName(typeof(CourseLearnTime), q.TimeStudy == null ? 0 : q.TimeStudy)
                                 };
                    return query3.ToList();
                default:
                    return null;

            }
        }

        public static IList SearchCourseByName(string strName)
        {
            var query = from q in CourseModel.GetTable()
                        where q.Name.StartsWith(strName)
                        select new
                        {
                            CourseID = q.ID,
                            CourseName = q.Name.Trim(),
                            Level = Enum.GetName(typeof(CourseLevel), q.Level),
                            LitmitPeople = q.LimitPeople,
                            StartTime = q.StartTime,
                            EndTime = q.EndTime,
                            TimeStudy = Enum.GetName(typeof(CourseLearnTime), q.TimeStudy)
                        };
            return query.ToList();
        }

        public static IList<string> GetAllCourseName()
        {
            var query = CourseModel.GetTable().Select(p => p.Name.Trim());
            return query.ToList();
        }

        public static CourseModel GetByPrimaryKey(object key)
        {
            var query = CourseModel.GetTable().Where(p => p.ID == (Guid)key);
            return query.FirstOrDefault();
        }

        public static CourseModel GetByName(string strName)
        {
            var query = CourseModel.GetTable().Where(p => p.Name == strName);
            return query.FirstOrDefault();
        }

        public static void UpdateCourse(CourseModel course)
        {
            course.Update();

        }

        public static void InsertCourse(CourseModel course)
        {
            course.ID = Guid.NewGuid();
            course.Insert();
        }

        public static bool DeleteCourse(Guid CourseID)
        {
            try
            {
                List<StudentModel> removeStudents = StudentModel.GetTable().Where(p => p.CourseID == CourseID).ToList();
                foreach (var item in removeStudents)
                {
                    item.Delete();
                }

                CourseModel removeCourse = CourseModel.GetTable().Where(p => p.ID == CourseID).First();
                removeCourse.Delete();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public static Dictionary<Guid, string> GetDictionaryCourseName()
        {
            try
            {
                var query = from q in CourseModel.GetTable()
                            select new
                            {
                                Key = q.ID,
                                Name = q.Name.Trim()
                            };
                return query.ToDictionary(p => p.Key, p => p.Name);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
